<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="org.internna.iwebmvc.model.ui.Filter" %>
<%@ page import="org.internna.iwebjtracker.spring.mvc.ProjectController" %>
<%@ taglib prefix="iwebmvc" uri="http://tags.iwebmvc.com" %>
<%@ taglib prefix="iwebjtracker" tagdir="/WEB-INF/tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<c-rt:set var="project" value="<%= ProjectController.PROJECT %>" />
<c-rt:set var="isAdmin" value="<%= ProjectController.PROJECT_ADMIN %>" />
<c-rt:set var="projectClass" value="<%= org.internna.iwebjtracker.model.DefaultProject.class %>" />
<c-rt:set var="moduleClass" value="<%= org.internna.iwebjtracker.model.DefaultModule.class %>" />
<c-rt:set var="versionClass" value="<%= org.internna.iwebjtracker.model.DefaultVersion.class %>" />
<c-rt:set var="sessionUser" value="<%= org.internna.iwebmvc.security.UserManager.SESSION_USER %>" />
<c-rt:set var="subsession_id" value="<%= org.internna.iwebmvc.spring.jee.IWebMvcCombinedFilter.SUB_SESSION_COOKIE %>" />
<c-rt:set var="subsession_path" value="<%= org.internna.iwebmvc.spring.jee.IWebMvcCombinedFilter.SUB_SESSION_COOKIE_PATH %>" />
<%
    List<Filter> filters = new ArrayList<Filter>(1);
    Filter filter = new Filter();
    filter.setPath("securityUser.enabled");
    filter.setType("BOOLEAN");
    filter.setFrom("true");
    filter.setRange(false);
    filters.add(filter);
    pageContext.setAttribute("enabledUsers", filters);
%>
<script type="text/javascript">
    <c:if test="${requestScope[isAdmin]}">
        var ${requestScope[project].alias}modulesData = {identifier: 'sequence', label: 'moduleName', items: [
            {
                sequence: 0, id: "<iwebmvc:value entity="${requestScope[project]}" path="id" encrypt="true" />", moduleName: "${requestScope[project].projectName}", level: -1, children: [
                <c:forEach items="${requestScope[project].modules}" var="module" varStatus="loop">
                    {
                        sequence: ${loop.index + 1}, moduleName: '${module.moduleName}', description: "${module.description}", level: 0, id: '<iwebmvc:value entity="${module}" path="id" encrypt="true" />'
                        <c:if test="${not empty module.components}">
                            ,children:[<c:forEach items="${module.components}" var="component" varStatus="innerLoop">
                                {
                                    sequence:${loop.index + 1}*100+${innerLoop.index + 1}, moduleName: '${component.moduleName}', description: "${component.description}", level: 1, id: '<iwebmvc:value entity="${component}" path="id" encrypt="true" />'
                                    <c:if test="${not empty component.components}">
                                        ,children:[<c:forEach items="${component.components}" var="subcomponent" varStatus="controlLoop">
                                            {
                                                sequence: ${loop.index + 1}*1000+${innerLoop.index + 1}*100+${controlLoop.index + 1}, moduleName: '${subcomponent.moduleName}', description: "${subcomponent.description}", level: 2, id: '<iwebmvc:value entity="${subcomponent}" path="id" encrypt="true" />'
                                            }<c:if test="${!controlLoop.last}">,</c:if>
                                        </c:forEach>]
                                    </c:if>
                                }<c:if test="${!innerLoop.last}">,</c:if>
                            </c:forEach>]
                        </c:if>
                    }<c:if test="${!loop.last}">,</c:if>
                </c:forEach>
            ]}
        ]};
        var ${requestScope[project].alias}modules = new dojo.data.ItemFileReadStore({data: ${requestScope[project].alias}modulesData});
        var ${requestScope[project].alias}membershipData = {identifier: 'sequence', label: 'name', items: [
            {
                sequence: 0, name: "<iwebmvc:message argMessage="ui.project.membership.team" />", children: [
                    {sequence: 1, name: "<iwebmvc:message argMessage="ui.project.membership.administrators" />", children: [
                        <c:forEach items="${requestScope[project].projectAdministrators}" var="admin" varStatus="loopAdms">
                            <c:if test="${!loopAdms.first}">,</c:if>{sequence: ${3 + loopAdms.index} * 100, name: "${admin.name}(${admin.username})"}
                        </c:forEach>
                    ]},
                    {sequence: 2, name: "<iwebmvc:message argMessage="ui.project.membership.developers" />", children: [
                        <c:forEach items="${requestScope[project].projectDevelopers}" var="dev" varStatus="loopDevs">
                            <c:if test="${!loopDevs.first}">,</c:if>{sequence: ${1000 + loopDevs.index} * 10000, name: "${dev.name}(${dev.username})"}
                        </c:forEach>
                    ]}
                ]
            }
        ]};
        var ${requestScope[project].alias}membership = new dojo.data.ItemFileReadStore({data: ${requestScope[project].alias}membershipData});
    </c:if>
    var ${requestScope[project].alias}versionsData = {identifier: 'sequence', label: 'label', items: [
        {
            sequence: 0, id: "<iwebmvc:value entity="${requestScope[project]}" path="id" encrypt="true" />", label: "${requestScope[project].projectName}", dueDate: null, level: -1, current: false, children: [
            <c:forEach items="${requestScope[project].allVersions}" var="version" varStatus="loopVersions">
                {
                    sequence: ${loopVersions.index + 1}, label: '${version.label}', current: ${version.currentVersion}, level: 0,
                    dueDate: <c:choose><c:when test="${version.dueDate == null}">null</c:when><c:otherwise>new Date(${version.dueDate.time})</c:otherwise></c:choose>, id: '<iwebmvc:value entity="${version}" path="id" encrypt="true" />',
                    <c:if test="${version.milestones != null}">children: [
                        <c:forEach items="${version.milestones}" var="milestone" varStatus="loopMilestones">
                            {
                                sequence: ${loopVersions.index + 1} * 1000 + ${loopMilestones.index + 1} * 10000, label: '${milestone.label}', current: ${milestone.currentVersion}, level: 1,
                                dueDate: <c:choose><c:when test="${milestone.dueDate == null}">null</c:when><c:otherwise>new Date(${milestone.dueDate.time})</c:otherwise></c:choose>, id: '<iwebmvc:value entity="${milestone}" path="id" encrypt="true" />'
                            }<c:if test="${!loopMilestones.last}">,</c:if>
                        </c:forEach>
                    ]</c:if>
                }<c:if test="${!loopVersions.last}">,</c:if>
            </c:forEach>
        ]}
    ]};
    var ${requestScope[project].alias}versions = new dojo.data.ItemFileReadStore({data: ${requestScope[project].alias}versionsData});
</script>
<div dojoType="dijit.layout.BorderContainer" gutters="false">
    <div dojoType="dijit.layout.ContentPane" region="top" style="height:25%;text-align:center">
        <div id="${requestScope[project].alias}name" class="projectNameContainer">
            ${requestScope[project].projectName}
        </div>
        <img id="${requestScope[project].alias}logo" src="<c:url value="/resource.iwebmvc?file=" /><c:choose><c:when test="${requestScope[project].logo != null}"><iwebmvc:encrypt code="${requestScope[project].logo.id}" /></c:when><c:otherwise>/images/nologo.jpg</c:otherwise></c:choose>&${requestScope[subsession_path]}=${requestScope[subsession_id]}" alt="logo" style="max-width:95%;margin-top:5px" />
    </div>
    <div dojoType="dijit.layout.AccordionContainer" region="center">
        <div dojoType="dijit.layout.AccordionPane" selected="true" title="<iwebmvc:message argMessage="ui.project.tasks" />">
            <div style="text-align:center;width:100%">
                <iwebmvc:hide requiredRoles="ROLE_PROJECT_CREATOR">
                    <iwebmvc:button label="ui.newproject" buttonCssStyle="text-align:center;width:160px">
                        resetProjectData("nproject");
                        openPopup("newprojectform");
                    </iwebmvc:button>
                 </iwebmvc:hide>
                <iwebmvc:button label="ui.project.changelog" buttonCssStyle="text-align:center;width:160px">
                    openChangeLogForm("${requestScope[project].alias}");
                </iwebmvc:button>
            </div>
        </div>
        <c:if test="${requestScope[isAdmin]}">
            <div dojoType="dijit.layout.AccordionPane" selected="true" title="<iwebmvc:message argMessage="ui.project.management" altText="ui.project.management" />">
                <div style="text-align:center;width:100%">
                    <c:choose>
                        <c:when test="${requestScope[project].open}">
                            <iwebmvc:button label="ui.project.settings" buttonCssStyle="text-align:center;width:160px">
                                openPopup("${requestScope[project].alias}");
                            </iwebmvc:button>
                            <iwebmvc:button label="ui.project.modules" buttonCssStyle="text-align:center;width:160px">
                                openPopup("${requestScope[project].alias}Modules");
                            </iwebmvc:button>
                            <iwebmvc:button label="ui.project.membership" buttonCssStyle="text-align:center;width:160px">
                                openPopup("${requestScope[project].alias}Membership");
                            </iwebmvc:button>
                            <iwebmvc:button label="ui.project.versions" buttonCssStyle="text-align:center;width:160px">
                                openPopup("${requestScope[project].alias}Versions");
                            </iwebmvc:button>
                            <iwebmvc:button buttonId="${requestScope[project].alias}Close" label="ui.project.close" buttonCssStyle="text-align:center;width:160px">
                                dijit.byId("${requestScope[project].alias}Close").setDisabled(true);
                                DefaultProjectManager.closeProject('${param.project}', function(data) {
                                    if (!data) {
                                        closeTab(dijit.byId("mainTabContainer"), "${requestScope[project].projectName}");
                                        updateDashboardGrids();
                                    } else {
                                        alert("Error: " + data);
                                        dijit.byId("${requestScope[project].alias}Close").setDisabled(false);
                                    }
                                });
                            </iwebmvc:button>
                        </c:when>
                        <c:otherwise>
                            <iwebmvc:button buttonId="${requestScope[project].alias}Open" label="ui.project.reopen" buttonCssStyle="text-align:center;width:160px">
                                dijit.byId("${requestScope[project].alias}Open").setDisabled(true);
                                DefaultProjectManager.reopenProject('${param.project}', function(data) {
                                    if (!data) {
                                        closeTab(dijit.byId("mainTabContainer"), "${requestScope[project].projectName}");
                                        updateDashboardGrids();
                                    } else {
                                        alert("Error: " + data);
                                        dijit.byId("${requestScope[project].alias}Open").setDisabled(false);
                                    }
                                });
                            </iwebmvc:button>
                        </c:otherwise>
                    </c:choose>
                </div>
            </div>
        </c:if>
        <div dojoType="dijit.layout.AccordionPane" title="<iwebmvc:message argMessage="ui.common.settings" />">
             <div style="text-align:center;width:100%;">
                 <iwebmvc:hide requiredRoles="ROLE_USER">
                    <iwebmvc:button label="ui.profile.edit" buttonCssStyle="text-align:center;width:160px">
                        openPopup("editprofileform");
                    </iwebmvc:button>
                </iwebmvc:hide>
            </div>
        </div>
    </div>
    <div dojoType="dijit.layout.ContentPane" region="bottom" style="height:25%;">
        <c:if test="${requestScope[isAdmin]}">
            <iwebmvc:popup id="${requestScope[project].alias}" title="ui.project.settings">
                <div class="projectSettings">
                    <span class="field">
                        <iwebmvc:textbox id="${requestScope[project].alias}" entity="${requestScope[project]}" path="projectName" />
                    </span>
                    <span class="field">
                        <iwebmvc:textbox id="${requestScope[project].alias}" entity="${requestScope[project]}" path="alias" />
                        <iwebmvc:help help="ui.project.settings.changealias" />
                    </span>
                    <span class="field">
                        <iwebmvc:textbox id="${requestScope[project].alias}" entity="${requestScope[project]}" path="description" />
                    </span>
                    <span class="field">
                        <iwebmvc:document id="${requestScope[project].alias}" entity="${requestScope[project]}" path="logo" />
                    </span>
                    <span class="field">
                        <iwebmvc:textbox id="${requestScope[project].alias}" entity="${requestScope[project]}" path="homePage" />
                    </span>
                    <span class="field">
                        <iwebmvc:checkbox id="${requestScope[project].alias}" entity="${requestScope[project]}" path="publicView" postLabel="false" />
                    </span>
                    <div style="width:100%;text-align:right;padding-top:5px">
                        <iwebmvc:button label="ui.project.settings.save" buttonIcon="/resource.iwebmvc?file=/images/preferences.png" >
                            editProjectSettings("<iwebmvc:encrypt code="${requestScope[project].id}" />", "${requestScope[project].alias}");
                        </iwebmvc:button>
                    </div>
                </div>
            </iwebmvc:popup>
            <iwebmvc:popup id="${requestScope[project].alias}Modules" title="ui.project.modules" onShow="dijit.byId('${requestScope[project].alias}ModuleTree').resize()">
                <div class="projectModules overclear">
                    <span class="float_left" style="display:inline-block;border:1px solid gray;padding:5px;">
                        <div class="label" style="margin-bottom:5px"><iwebmvc:message argMessage="ui.project.modules.current" /></div>
                        <div dojoType="dijit.tree.ForestStoreModel" jsId="${requestScope[project].alias}model" store="${requestScope[project].alias}modules" childrenAttrs="children"></div>
                        <div dojoType="dijit.Tree" id="${requestScope[project].alias}ModuleTree" model="${requestScope[project].alias}model" showRoot="false" openOnClick="false" autoExpand="true">
                            <script type="dojo/method" event="onClick" args="item">
                                onComponentClick(item, "${requestScope[project].alias}");
                            </script>
                            <script type="dojo/method" event="getIconClass" args="item, opened">
                                return item.sequence !== undefined && item.sequence > 0 ? "moduleIcon" : "projectIcon" + currentTheme;
                            </script>
                        </div>
                    </span>
                    <div class="float_right">
                        <div style="width:375px;border:1px solid gray;padding:5px;margin-left:5px">
                            <div style="width:100%">
                                <input type="hidden" id="${requestScope[project].alias}moduleparent" />
                                <iwebmvc:textbox id="${requestScope[project].alias}module" entityClass="${moduleClass}" path="moduleName" />
                                <iwebmvc:textbox id="${requestScope[project].alias}module" entityClass="${moduleClass}" path="description" />
                            </div>
                            <div style="width:100%;text-align:right;margin-top:5px">
                                <span id="${requestScope[project].alias}modulesSaveLoadingContainer" style="display:none"><img src='<c:url value="resource.iwebmvc?file=images/loading.gif" />&${requestScope[subsession_path]}=${requestScope[subsession_id]}' alt='' /></span>
                                <iwebmvc:button buttonId="${requestScope[project].alias}modulesSaveLoading" label="ui.project.modules.save" buttonIcon="/resource.iwebmvc?file=/images/savemodule.png" disabled="true">
                                    changeModule(dojo.byId("${requestScope[project].alias}moduleparent").value, dijit.byId("moduleName${requestScope[project].alias}module").getValue(), dijit.byId("description${requestScope[project].alias}module").getValue(), ${requestScope[project].alias}modulesData, ${requestScope[project].alias}model, "${requestScope[project].alias}");
                                </iwebmvc:button>
                            </div>
                        </div>
                        <div style="width:375px;border:1px solid gray;padding:5px;margin-left:5px;margin-top:10px">
                            <div style="width:100%">
                                <input type="hidden" id="${requestScope[project].alias}moduleparentnewlevel" value="-1" />
                                <iwebmvc:textbox id="${requestScope[project].alias}modulenew" entityClass="${moduleClass}" path="moduleName" />
                                <iwebmvc:textbox id="${requestScope[project].alias}modulenew" entityClass="${moduleClass}" path="description" />
                            </div>
                            <div style="width:100%;text-align:right;margin-top:5px">
                                <span id="${requestScope[project].alias}modulesNewLoadingContainer" style="display:none"><img src='<c:url value="resource.iwebmvc?file=images/loading.gif" />&${requestScope[subsession_path]}=${requestScope[subsession_id]}' alt='' /></span>
                                <iwebmvc:button buttonId="${requestScope[project].alias}modulesNewLoading" label="ui.project.modules.new" buttonIcon="/resource.iwebmvc?file=/images/newmodule.gif">
                                    onNewComponent("${requestScope[project].alias}");
                                </iwebmvc:button>
                            </div>
                        </div>
                    </div>
                </div>
            </iwebmvc:popup>
            <iwebmvc:popup id="${requestScope[project].alias}Membership" title="ui.project.membership" onShow="dijit.byId('${requestScope[project].alias}MembershipTree').resize()">
                <div>
                    <div dojoType="dijit.tree.ForestStoreModel" jsId="${requestScope[project].alias}membershipModel" store="${requestScope[project].alias}membership" childrenAttrs="children"></div>
                    <div dojoType="dijit.Tree" id="${requestScope[project].alias}MembershipTree" model="${requestScope[project].alias}membershipModel" showRoot="false" openOnClick="false" autoExpand="true">
                        <script type="dojo/method" event="getIconClass" args="item, opened">
                            return item.sequence < 3 ? "teamIcon" : item.sequence > 10000 ? "developerIcon" : "adminIcon";
                        </script>
                    </div>
                </div>
                <div>
                    <iwebmvc:locator id="${requestScope[project].alias}" entityClass="${projectClass}" path="developers" pages="5" rows="5" baseColumnWidth="100" currentDepthLevel="0" maxDepthLevel="1" filters="${enabledUsers}" />
                    <span style="display:inline-block">
                        <span class="label"></span>
                        <span>
                            <select id="${requestScope[project].alias}MembershipType" dojoType="dijit.form.FilteringSelect">
                                <option value="0"><iwebmvc:message argMessage="ui.project.membership.developer" altText="Developer" /></option>
                                <option value="1"><iwebmvc:message argMessage="ui.project.membership.administrator" altText="Administrator" /></option>
                            </select>
                        </span>
                    </span>
                    <div style="width:100%;text-align:right;margin-top:5px">
                        <span id="${requestScope[project].alias}membershipNewLoadingContainer" style="display:none"><img src='<c:url value="resource.iwebmvc?file=images/loading.gif" />&${requestScope[subsession_path]}=${requestScope[subsession_id]}' alt='' /></span>
                        <iwebmvc:button buttonId="${requestScope[project].alias}membership" label="ui.project.membership.new" buttonIcon="/resource.iwebmvc?file=/images/adduser.gif">
                            addMember('${requestScope[project].alias}', '<iwebmvc:encrypt code="${requestScope[project].id}" />', ${requestScope[project].alias}membershipData, ${requestScope[project].alias}membershipModel);
                        </iwebmvc:button>
                    </div>
                </div>
            </iwebmvc:popup>
        </c:if>
        <iwebmvc:popup id="${requestScope[project].alias}Versions" title="ui.project.versions" onShow="dijit.byId('${requestScope[project].alias}VersionTree').resize()">
            <div class="projectModules overclear">
                <span id="${requestScope[project].alias}VersionTreeContainer" class="float_left" style="display:inline-block;border:1px solid gray;padding:5px;">
                    <div class="label" style="margin-bottom:5px"><iwebmvc:message argMessage="ui.project.versions.current" /></div>
                    <div dojoType="dijit.tree.ForestStoreModel" jsId="${requestScope[project].alias}modelVersion" store="${requestScope[project].alias}versions" childrenAttrs="children"></div>
                    <div id="${requestScope[project].alias}VersionTree" dojoType="dijit.Tree" model="${requestScope[project].alias}modelVersion" showRoot="false" openOnClick="false" autoExpand="true">
                        <script type="dojo/method" event="onClick" args="item">
                            onVersionClick("${requestScope[project].alias}", item);
                        </script>
                        <script type="dojo/method" event="getIconClass" args="item, opened">
                            var level = dojo.isArray(item.level) ? item.level[0] : item.level;
                            if (dojo.isArray(item.current) ? item.current[0] : item.current) return "currentIcon_" + currentTheme;
                            return level === -1 ? "projectIcon" + currentTheme : level > 0 ? "milestoneIcon" : "versionIcon";
                        </script>
                    </div>
                </span>
                <c:if test="${requestScope[isAdmin]}">
                    <div class="float_right">
                        <div style="width:375px;border:1px solid gray;padding:5px;margin-left:5px">
                            <div style="width:100%">
                                <input type="hidden" id="${requestScope[project].alias}versionparent" />
                                <iwebmvc:textbox id="${requestScope[project].alias}version" entityClass="${versionClass}" path="label" />
                                <iwebmvc:date id="${requestScope[project].alias}version" entityClass="${versionClass}" path="dueDate" />
                            </div>
                            <div style="width:100%;text-align:right;margin-top:5px">
                                <span id="${requestScope[project].alias}versionsLoadingContainer" style="display:none"><img src='<c:url value="resource.iwebmvc?file=images/loading.gif" />&${requestScope[subsession_path]}=${requestScope[subsession_id]}' alt='' /></span>
                                <iwebmvc:button buttonId="${requestScope[project].alias}versionsCurrentLoading" label="ui.project.versions.setcurrent" buttonIcon="/resource.iwebmvc?file=/images/common/openarrow_${sessionScope[sessionUser].theme}.gif&${requestScope[subsession_path]}=${requestScope[subsession_id]}" disabled="true">
                                    setAsCurrentVersion("${requestScope[project].alias}");
                                </iwebmvc:button>
                                <iwebmvc:button buttonId="${requestScope[project].alias}versionsSaveLoading" label="ui.project.versions.save" buttonIcon="/resource.iwebmvc?file=/images/common/saveversion_${sessionScope[sessionUser].theme}.gif&${requestScope[subsession_path]}=${requestScope[subsession_id]}" disabled="true">
                                    saveVersionChanges("${requestScope[project].alias}");
                                </iwebmvc:button>
                            </div>
                        </div>
                        <div style="width:375px;border:1px solid gray;padding:5px;margin-left:5px;margin-top:10px;">
                            <div style="width:100%">
                                <input type="hidden" id="${requestScope[project].alias}moduleparent" />
                                <iwebmvc:textbox id="${requestScope[project].alias}versionNew" entityClass="${versionClass}" path="label" />
                                <iwebmvc:date id="${requestScope[project].alias}versionNew" entityClass="${versionClass}" path="dueDate" />
                            </div>
                            <div style="width:100%;text-align:right;margin-top:5px">
                                <span id="${requestScope[project].alias}versionsNewLoadingContainer" style="display:none"><img src='<c:url value="resource.iwebmvc?file=images/loading.gif" />&${requestScope[subsession_path]}=${requestScope[subsession_id]}' alt='' /></span>
                                <iwebmvc:button buttonId="${requestScope[project].alias}versionsNewLoading" label="ui.project.versions.new" buttonIcon="/resource.iwebmvc?file=/images/clock_${sessionScope[sessionUser].theme}.gif">
                                    newVersionMilestone("${requestScope[project].alias}");
                                </iwebmvc:button>
                            </div>
                        </div>
                    </div>
                </c:if>
            </div>
        </iwebmvc:popup>
    </div>
</div>
<script type="text/javascript">
function closeTab(container, title) {
    if (container) {
        var child = isTabOpen(container, title);
        if (child) container.removeChild(child);
    }
}
</script>
